using System.Windows.Forms;

namespace SynoManager.FileStation
{
    public class FileInfo : IRemoteLocation
    {
        private readonly FileStationService service;
        private readonly FileStationClient.GetFileResultItem file;

        internal FileInfo(FileStationService service, FileStationClient.GetFileResultItem file)
        {
            this.service = service;
            this.file = file;
        }

        public string Name
        {
            get { return file.filename; }
            set 
            {
                service.client.Rename(ParentPath, file.filename, value);
                file.filename = value;
            }
        }

        public string Path
        {
            get { return file.path; }
        }

        public string RelativePath
        {
            get { return Path.Substring(Path.IndexOf('/', 1)); }
        }

        /// <summary>
        /// Gets the (relative) path of the parent folder
        /// </summary>
        public string ParentPath
        {
            get
            {
                string relpath = RelativePath;
                string result = relpath.Substring(0, relpath.LastIndexOf('/'));
                return result;
            }
        }


        public bool IsExtractable
        {
            get { return file.is_compressed; }
        }

        public long Size
        {
            get
            {
                return ByteSize.Parse(file.filesize);
            }
        }

        public string PermissionString
        {
            get
            {
                // TODO: Return permissions object (enum?)
                return file.privilege_str;
            }
        }

        public string Owner
        {
            get
            {
                return file.owner;
            }
        }

        public string LastModified
        {
            get
            {
                // TODO: Return DateTime
                return file.mod_time;
            }
        }
    }
}